package tech.aistar.corejava.day15.synchronizeddemo;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/8/3
 * @Version: 1.0
 */
public class AccountDemo {
    public static void main(String[] args) {
        Runnable r = new AccountThread();

        Thread husband =new Thread(r);
        husband.setName("文章...");

        Thread wife = new Thread(r);
        wife.setName("马伊琍");
        husband.start();
        wife.start();
    }

}
class AccountThread implements Runnable {
    private Account acc = new Account();

    @Override
    public void run() {
        for (int i = 0; i < 5; i++) {
            withdraw(10);
        }

    }

    private void withdraw(double money) {
        synchronized (acc){
            System.out.println(Thread.currentThread().getName()+"正在取款...");
            acc.withdraw(money);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"完成取款....");
        }
    }
}
